package com.lxmes.device.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.device.mapper.FwSettlementRecordMapper;
import com.lxmes.device.domain.FwSettlementRecord;
import com.lxmes.device.service.IFwSettlementRecordService;

/**
 * 维保结算Service业务层处理
 * 
 * @author zhangzhiyu
 * @date 2023-05-11
 */
@Service
public class FwSettlementRecordServiceImpl implements IFwSettlementRecordService 
{
    @Autowired
    private FwSettlementRecordMapper fwSettlementRecordMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询维保结算
     * 
     * @param id 维保结算主键
     * @return 维保结算
     */
    @Override
    public FwSettlementRecord selectFwSettlementRecordById(Long id)
    {
        return fwSettlementRecordMapper.selectFwSettlementRecordById(id);
    }

    /**
     * 查询维保结算列表
     * 
     * @param fwSettlementRecord 维保结算
     * @return 维保结算
     */
    @Override
    public List<FwSettlementRecord> selectFwSettlementRecordList(FwSettlementRecord fwSettlementRecord)
    {
        return fwSettlementRecordMapper.selectFwSettlementRecordList(fwSettlementRecord);
    }

    /**
     * 新增维保结算
     * 
     * @param fwSettlementRecord 维保结算
     * @return 结果
     */
    @Override
    public int insertFwSettlementRecord(FwSettlementRecord fwSettlementRecord)
    {
        return fwSettlementRecordMapper.insertFwSettlementRecord(fwSettlementRecord);
    }

    /**
     * 修改维保结算
     * 
     * @param fwSettlementRecord 维保结算
     * @return 结果
     */
    @Override
    public int updateFwSettlementRecord(FwSettlementRecord fwSettlementRecord)
    {
        return fwSettlementRecordMapper.updateFwSettlementRecord(fwSettlementRecord);
    }

    /**
     * 批量删除维保结算
     * 
     * @param ids 需要删除的维保结算主键
     * @return 结果
     */
    @Override
    public int deleteFwSettlementRecordByIds(Long[] ids)
    {
        return fwSettlementRecordMapper.deleteFwSettlementRecordByIds(ids);
    }

    /**
     * 删除维保结算信息
     * 
     * @param id 维保结算主键
     * @return 结果
     */
    @Override
    public int deleteFwSettlementRecordById(Long id)
    {
        return fwSettlementRecordMapper.deleteFwSettlementRecordById(id);
    }
    @Override
    public String imporIteminfo(List<FwSettlementRecord> fwSettlementRecordList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(fwSettlementRecordList) || fwSettlementRecordList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (FwSettlementRecord fwSettlementRecord : fwSettlementRecordList)
        {
            try
            {
                BeanValidators.validateWithException(validator, fwSettlementRecord);
                fwSettlementRecord.setCreateBy(operName);
                fwSettlementRecord.setUpdateBy(operName);
                this.insertFwSettlementRecord(fwSettlementRecord);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
